<?php

class DealerController extends Controller {

	public function goodsAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 20));
		$categoryId = intval(Request::getParams('categoryId', 0));
		$where = $value = [];
		$where[] = 'g.status=?';
		$value[] = 1;
		$where[] = 's.type=?';
		$value[] = 2;
		if ($categoryId) {
			$where[] = 'r.category_id=?';
			$value[] = $categoryId;
		}
		//获得
		$ruleInfo = self::model('point_rule')->getOne('id=?', 1);
		$daili = self::model('user_apply')->getOne('uid=?', $user_info['uid']);
		if ($daili['type'] == 1) {
			$reg_rebate1 = $ruleInfo['reg_rebate'];
			$shop_rebate1 = $ruleInfo['shop_rebate'];
			$derler_rebate1 = $ruleInfo['derler_rebate'];
			$daili_rebate1 = 0;
		} else {
			$reg_rebate1 = $ruleInfo['regs_rebate'];
			$shop_rebate1 = $ruleInfo['shops_rebate'];
			$derler_rebate1 = $ruleInfo['derlers_rebate'];
			$daili_rebate1 = $ruleInfo['daili_rebate'];
		}
		$data = self::model('goods')->dealerGoods($where, $value, $categoryId, $orderBy, $page, $pageSize);
		$temp = [];
		foreach ($data as $key => $val) {
			$daili_rebate = '';
			$profit = floatval($val['profit']);
			if ($daili['type'] == 2) {
				$daili_rebate = round($profit * $daili_rebate1 / 100, 2);
			}
			$reg_rebate = round($profit * $reg_rebate1 / 100, 2);
			$shop_rebate = round($profit * $shop_rebate1 / 100, 2);
			$derler_rebate = round($profit * $derler_rebate1 / 100, 2);
			$temp[$key]['goodsId'] = $val['goods_id'];
			$temp[$key]['name'] = $val['name'];
			$temp[$key]['image'] = $val['image'];
			$temp[$key]['profit'] = $val['profit'];
			$temp[$key]['sales_price'] = $val['sales_price'];
			$temp[$key]['reg_rebate'] = $reg_rebate;
			$temp[$key]['shop_rebate'] = $shop_rebate;
			$temp[$key]['system_rebate'] = $derler_rebate;
			$temp[$key]['dianzhu_rebate'] = $daili_rebate;
			$temp[$key]['total_rebate'] = $reg_rebate + $shop_rebate + $derler_rebate + floatval($daili_rebate);
		}
		$result = [
			'page' => $page,
			'pageSize' => $pageSize,
			'list' => $temp,
		];
		Response::ajax(200, 'success', $result);
	}

	public function pointAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$uid = $user_info['uid'];
		$user_infos = self::model('user')->getOne('uid=?', $uid);
		if ($uid) {
			$where[] = 'uid=?';
			$value[] = $uid;
			$url['uid'] = $uid;
		} else {
			Response::showMsg('用户信息查看失败');
		}
		$total = self::model('user_point')->count('id', $where, $value);
		$data = self::model('user_point')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value);
		$temp = [];
		foreach ($data as $key => $val) {
			$temp[$key]['type'] = intval($val['type']);
			$temp[$key]['point'] = floatval($val['point']);
			$temp[$key]['title'] = $val['title'];
			$temp[$key]['addtime'] = date('Y-m-d', $val['add_time']);
			$temp[$key]['orderno'] = $val['orderno'];
		}
		$result = array(
			'page' => $page,
			'pageSize' => $pageSize,
			'points' => floatval($user_infos['points']),
			'list' => $temp,
		);
		Response::ajax(200, 'success', $result);
	}

	public function orderAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$orderStatus = intval(Request::getParams('orderStatus', -1));
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 20));
		$uid = $user_info['uid'];
		//$uid = 20;
		$where = $value = [];
		$where[] = 'is_delete=?';
		$value[] = 0;
		if ($orderStatus != -1) {
			if ($orderStatus == 0) {
//待支付
				$where[] = 'o.order_status=?';
				$value[] = $orderStatus;
			} elseif ($orderStatus == 1) {
				//进行中:
				$status = orderModel::ORDER_STATUS_PAIED . ',' . orderModel::ORDER_STATUS_POST . ',' . orderModel::ORDER_STATUS_USER_SIGN . ',' . orderModel::ORDER_STATUS_UNCOMMENT;
				$where[] = "o.order_status in ($status)";
			} elseif ($orderStatus == 2) {
				//已完成
				$where[] = 'o.order_status=?';
				$value[] = orderModel::ORDER_STATUS_FINISH;
			}
		}
		Response::ajax(200, 'success',
			self::model('order')->getUserOrderLists($where, $value, $uid, $page, $pageSize));
	}

	public function starAction() {
		$user_info = self::model('user')->checkLogin();

		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$limit = ($page - 1) * $pageSize;
		$sql = "select a.realname,a.uid,u.avatar,u.autograph,u.fans_total,u.nickname,a.image,0 as is_fans from wf_user_apply a left join wf_user u on a.uid = u.uid";
		$sql .= " where a.isstar=1 order by a.starsort asc limit {$limit}, {$pageSize}";
		$list = self::model('user_apply')->query($sql)->fetchAll();
		foreach ($list as $key1 => $value1) {
			$list[$key1]['realname'] = strval($value1['realname']);
			$list[$key1]['nickname'] = strval($value1['nickname']) ? strval($value1['nickname']) : strval($value1['realname']);
			$list[$key1]['uid'] = intval($value1['uid']);
			$list[$key1]['avatar'] = strval($value1['avatar']);
			$list[$key1]['autograph'] = strval($value1['autograph']);
			$list[$key1]['fans_total'] = intval($value1['fans_total']);
			$list[$key1]['image'] = strval($value1['image']);
			$list[$key1]['is_fans'] = 0;
		}
		if ($user_info) {
			$relate = self::model('user_relate')->getAll('uid=?', $user_info['uid']);
			$temp = [];
			if ($relate) {
				foreach ($relate as $key => $value) {
					$temp[] = $value['to_uid'];
				}
				foreach ($list as $key1 => $value1) {
					if (in_array($value1['uid'], $temp)) {
						$list[$key1]['is_fans'] = 1;
					}
				}
			}
		}

		Response::ajax(200, 'success', $list);
	}

	public function InfoAction() {
		$user_info = self::model('user')->checkLogin();
		$puid = Request::getParams('puid', 0);
		$list = self::model('user')->findById($puid);
		if (!$list) {
			Response::ajax(NORMAL_ERROR, '参数有误,查看失败');
		}
		$list['is_fans'] = 0;
		if ($user_info) {
			$relate = self::model('user_relate')->getAll('uid=?', $user_info['uid']);
			$temp = [];
			if ($relate) {
				foreach ($relate as $key => $value) {
					$temp[] = $value['to_uid'];
				}
				if (in_array($puid, $temp)) {
					$list['is_fans'] = 1;
				}
			}
		}
		if ($list['type'] == 3 || $list['type'] == 4) {
			$flag = self::model('user_apply')->getOne('uid=?', $puid);
			$templist['realname'] = $flag['realname'];
			$list['image'] = $flag['image'];
		} else {
			$templist['realname'] = $list['nickname'];
		}
		$templist['nickname'] = $list['nickname'];
		$templist['uid'] = $list['uid'];
		$templist['avatar'] = $list['avatar'];
		$templist['autograph'] = $list['autograph'];
		$templist['image'] = $list['image'];
		$templist['fans_total'] = $list['fans_total'];
		$templist['is_fans'] = $list['is_fans'];
		Response::ajax(200, 'success', $templist);
	}

	public function bindDealerAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$puid = Request::getParams('puid');
		$pmobile = Request::getParams('pMobile');
		if (!$puid && !$pmobile) {
			Response::ajax(NORMAL_ERROR, '请选择明星店主或者输入邀请码');
		}
		$user = self::model('user')->getOne('uid=?', $user_info['uid']);
		if ($user['puid']) {
			Response::ajax(NORMAL_ERROR, '您已经绑定过店主,不能重复绑定');
		}
		if ($user['type'] != 1) {
			Response::ajax(NORMAL_ERROR, '该账号不能绑定明星店主');
		}
		$flag = '';
		if ($puid) {
			$flag = self::model('user_apply')->getOne('uid=?', $puid);
		} else {
			$flag = self::model('user_apply')->getOne('mobile=?', $pmobile);
		}
		if (!$flag) {
			Response::ajax(NORMAL_ERROR, '绑定失败，该店主不存在');
		}
		$data = [];
		$data['puid'] = $flag['uid'];
		$data['ptime'] = time();
		$result = self::model('user')->updateById($user_info['uid'], $data);
		if ($result) {
			$data = [];
			$data['uid'] = $user_info['uid'];
			$data['puid'] = $flag['uid'];
			$data['addtime'] = time();
			$data['type'] = 0;
			self::model('user_applyrecord')->insert($data, true);
			Response::ajax(200, '绑定成功!');
		} else {
			Response::ajax(NORMAL_ERROR, '绑定失败,请重试');
		}
	}

	public function statisticsAction() {
		$startDate = Request::getParams('startDate', '');
		$endDate = Request::getParams('endDate', '');

		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		//$user_info['uid'] = 64;
		$t = time();
		$start = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));
		$end = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));
		$info = self::model('user_profit')->query("SELECT count(1) as count1,sum(w.count) as count,sum(price) as price,sum(sale_price) as sale_price FROM (
SELECT buid,sum(price) as price,sum(sale_price) as sale_price,count(1) as count  FROM wf_user_profit WHERE add_time > " . $start . " AND add_time < " . $end . " AND uid=" . $user_info['uid'] . " GROUP BY buid ) w")->fetchAll();
		if ($info) {
			$unitPrice = 0;
			if ($info[0]['sale_price'] > 0) {
				$unitPrice = floatval($info[0]['sale_price']) / intval($info[0]['count1']);
			}
			$data = [
				'todayProfit' => floatval($info[0]['price']),
				'todayUid' => intval($info[0]['count1']),
				'todayOrder' => intval($info[0]['count']),
				'todayAmount' => floatval($info[0]['sale_price']),
				'todayUnitPrice' => round($unitPrice, 2),
			];
		} else {
			$data = [
				'todayProfit' => 0,
				'todayUid' => 0,
				'todayOrder' => 0,
				'todayAmount' => 0,
				'todayUnitPrice' => 0,
			];
		}

		$reg = self::model('user')->query("SELECT count(1) as count FROM wf_user WHERE puid =210 and add_time > '" . date("Y-m-d H:i:s", $start) . "' AND add_time < '" . date("Y-m-d H:i:s", $end) . "'")->fetchAll();
		if ($reg) {
			$data['todayReg'] = $reg[0]['count'];
		} else {
			$data['todayReg'] = 0;
		}

		//获取总价

		if ($startDate) {
			$startDate = strtotime($startDate . " 00:00:00");
			$endDate = strtotime($endDate . " 23:59:59");
		} else {
			$startDate = mktime(0, 0, 0, date('m'), 1, date('Y'));
			$endDate = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
		}

		$info = self::model('user_profit')->query("SELECT count(1) as count1,sum(w.count) as count,sum(price) as price,sum(sale_price) as sale_price FROM (
SELECT buid,sum(price) as price,sum(sale_price) as sale_price,count(1) as count  FROM wf_user_profit WHERE add_time > " . $startDate . " AND add_time < " . $endDate . " AND uid=" . $user_info['uid'] . " GROUP BY buid ) w")->fetchAll();
		if ($info) {
			$unitPrice = 0;
			if ($info[0]['sale_price'] > 0) {
				$unitPrice = floatval($info[0]['sale_price']) / intval($info[0]['count1']);
			}

			$data['totalProfit'] = floatval($info[0]['price']);
			$data['totalUid'] = intval($info[0]['count1']);
			$data['totalOrder'] = intval($info[0]['count']);
			$data['totalAmount'] = floatval($info[0]['sale_price']);
			$data['totalUnitPrice'] = round($unitPrice, 2);

		} else {

			$data['totalProfit'] = 0;
			$data['totalUid'] = 0;
			$data['totalOrder'] = 0;
			$data['totalAmount'] = 0;
			$data['totalUnitPrice'] = 0;

		}

		$reg = self::model('user')->query("SELECT count(1) as count FROM wf_user WHERE puid =210 and add_time > '" . date("Y-m-d H:i:s", $startDate) . "' AND add_time < '" . date("Y-m-d H:i:s", $endDate) . "'")->fetchAll();
		if ($reg) {
			$data['totalReg'] = $reg[0]['count'];
		} else {
			$data['totalReg'] = 0;
		}
		Response::ajax(200, 'success', $data);
	}

	/**
	 *
	 * @DateTime:	2018-08-06 21:43:35
	 * @Author:  cc
	 */
	public function getImageAction() {
		$rule = self::model('point_rule')->getOne('id=?', 1);
		$data['image'] = $rule['shop_image'];
		Response::ajax(200, 'success', $data);
	}

	/**
	 *
	 * @DateTime:	2018-08-06 21:43:35
	 * @Author:  cc
	 */
	public function getdealerAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$flag = self::model('user_apply')->getOne('uid=?', $user_info['uid']);
		$data['realName'] = $flag['realname'];
		$data['mobile'] = $flag['mobile'];
		$time = $flag['addtime'] + 31536000;
		$data['privilegedTime'] = date('Y-m-d', $time);
		Response::ajax(200, 'success', $data);
	}

	public function profitAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$uid = $user_info['uid'];
		//$uid = 20;
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 20));
		$where = $value = [];
		$where[] = 'uid=?';
		$value[] = $uid;
		Response::ajax(200, 'success', self::model('user_profit')->getList($where, $value, $page, $pageSize));
	}
}
